package com.haixiaoke.saas.tenant.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.config.SmsConfig;
import com.haixiaoke.saas.common.constant.HouseConstants;
import com.haixiaoke.saas.common.constant.TenantConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.common.utils.sms.SmsUtils;
import com.haixiaoke.saas.fadada.service.FaDaDaService;
import com.haixiaoke.saas.tenant.domain.ElectronicVisa;
import com.haixiaoke.saas.tenant.domain.Tenant;
import com.haixiaoke.saas.tenant.mapper.ElectronicVisaMapper;
import com.haixiaoke.saas.tenant.service.IElectronicVisaService;
import com.haixiaoke.saas.tenant.service.ITenantService;
import com.haixiaoke.saas.house.domain.House;
import com.haixiaoke.saas.user.domain.UserBusiness;
import com.haixiaoke.saas.user.service.IUserBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企业电子签信息Service业务层处理
 *
 * @author panda
 * @date 2024-05-29
 */
@Service
public class ElectronicVisaServiceImpl implements IElectronicVisaService {
    @Autowired
    private ElectronicVisaMapper electronicVisaMapper;
    @Autowired
    private FaDaDaService faDaDaService;
    @Autowired
    private SmsUtils smsUtils;
    @Autowired
    private SmsConfig smsConfig;
    @Autowired
    private IUserBusinessService userBusinessService;

    /**
     * 查询企业电子签信息
     *
     * @param visaId 企业电子签信息主键
     * @return 企业电子签信息
     */
    @Override
    public ElectronicVisa selectElectronicVisaByVisaId(Long visaId) {
        return electronicVisaMapper.selectElectronicVisaByVisaId(visaId);
    }

    /**
     * 查询企业电子签信息列表
     *
     * @param electronicVisa 企业电子签信息
     * @return 企业电子签信息
     */
    @Override
    public List<ElectronicVisa> selectElectronicVisaList(ElectronicVisa electronicVisa) {
        return electronicVisaMapper.selectElectronicVisaList(electronicVisa);
    }

    /**
     * 新增企业电子签信息
     *
     * @param electronicVisa 企业电子签信息
     * @return 结果
     */
    @Override
    public int insertElectronicVisa(ElectronicVisa electronicVisa) {
        electronicVisa.setCreateTime(DateUtils.getNowDate());
        return electronicVisaMapper.insertElectronicVisa(electronicVisa);
    }

    /**
     * 修改企业电子签信息
     *
     * @param electronicVisa 企业电子签信息
     * @return 结果
     */
    @Override
    public int updateElectronicVisa(ElectronicVisa electronicVisa) {
        electronicVisa.setUpdateTime(DateUtils.getNowDate());
        return electronicVisaMapper.updateElectronicVisa(electronicVisa);
    }

    /**
     * 批量删除企业电子签信息
     *
     * @param visaIds 需要删除的企业电子签信息主键
     * @return 结果
     */
    @Override
    public int deleteElectronicVisaByVisaIds(Long[] visaIds) {
        return electronicVisaMapper.deleteElectronicVisaByVisaIds(visaIds);
    }

    /**
     * 删除企业电子签信息信息
     *
     * @param visaId 企业电子签信息主键
     * @return 结果
     */
    @Override
    public int deleteElectronicVisaByVisaId(Long visaId) {
        return electronicVisaMapper.deleteElectronicVisaByVisaId(visaId);
    }

    @Override
    public ElectronicVisa selectElectronicVisaByTenantId(String tenantId) {
        return electronicVisaMapper.selectElectronicVisaByTenantId(tenantId);
    }

    @Override
    public ElectronicVisa selectElectronicVisaByCooperationType(House house) {
        return this.selectElectronicVisaByTenantId(house.getTenantId());
    }

    @Override
    public AjaxResult sendSmsAuth(String tenantId) {
        UserBusiness userBusiness = userBusinessService.selectUserManage(tenantId);
        if (StrUtil.isBlank(userBusiness.getPhonenumber())) {
            return AjaxResult.error("企业的联系方式不能为空");
        }
        ElectronicVisa electronicVisa = electronicVisaMapper.selectElectronicVisaByTenantId(tenantId);
        if (ObjectUtil.isEmpty(electronicVisa)) {
            electronicVisa = new ElectronicVisa();
            electronicVisa.setCreateTime(DateUtils.getNowDate());
            electronicVisa.setTenantId(tenantId);
            electronicVisaMapper.insertElectronicVisa(electronicVisa);
        }
        if (StrUtil.equals(TenantConstants.AUTH_TYPE_YES, electronicVisa.getAuthType())) {
            return AjaxResult.error("电子签已开通,请勿重复操作");
        }
        String corpAuthUrl = faDaDaService.getCorpAuthUrl(tenantId, userBusiness.getPhonenumber());
        boolean result = smsUtils.sendTemplateSmsWithData(userBusiness.getPhonenumber(), smsConfig.getFadadaAuthInformTemplateCode(), new String[]{corpAuthUrl});
        if (result) {
            return AjaxResult.success("短信发送成功,请尽快授权");
        }
        return AjaxResult.error("短信发送失败,请联系管理员");
    }

}
